/**
 * Created by yan_my on 2016/12/9.
 */
import java.sql.*;

public class DataBase {
    public static void main(String[] args) throws Exception {
        try {
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载驱动程序");

            String url = "jdbc:mysql://localhost:3306/homework_4";
            String name = "root";
            String password = "23336666";

            Connection con = DriverManager.getConnection(url, name, password);
            Statement sta = con.createStatement();
            System.out.println("已连接到数据库");

            //address表添加接受邮件邮箱地址
            String sql2 = "INSERT INTO address(address) VALUES(?)";
            PreparedStatement pre = con.prepareStatement(sql2);
            PreparedStatement pre1 = con.prepareStatement(sql2);
            pre.setString(1,"843901980@qq.com");
            pre1.setString(1,"yan_ymy@163.com");
            pre.addBatch();
            pre.executeUpdate();
            pre1.addBatch();
            pre1.executeUpdate();

            //address表查询已添加邮箱地址
            String sql1 = "SELECT * FROM address order by id";
            ResultSet rs = sta.executeQuery(sql1);
            System.out.println("id" + "\t" + "address");
            while (rs.next()) {
                System.out.print(rs.getInt(1) + "\t");
                System.out.println(rs.getString(2) + "\t");
            }

            pre.close();
            pre1.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}